package com.ejlchina.tool.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.core.widget.doAfterTextChanged
import androidx.navigation.fragment.findNavController
import com.adolf.androiddemo.R
import com.adolf.androiddemo.databinding.FragmentFanControlBinding
import com.ejlchina.tool.base.BaseBindingFragment2
import com.ejlchina.tool.constant.REQUEST_FAN_CONTROL_KEY
import com.ejlchina.tool.enums.FanControlEnum
import com.ejlchina.tool.util.LOG_TAG
import kotlin.properties.Delegates

class FanControlFragment: BaseBindingFragment2<FragmentFanControlBinding>() {

    private var selectFanMode :Int by Delegates.observable(-1){ _,_,newValue ->
        setFanStateUI()
    }
    private var fanTemperature :Int by Delegates.observable(0){ _,_,newValue ->
        setFanStateUI()
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
        initData()
    }

    private fun initViews(){
        setHeadTitle("风扇控制")
        setRightIconVisibility(View.GONE)
        initListeners()
    }

    private fun initData(){
        setFanState()
    }

    private fun setFanState(){
        arguments?.let {
            selectFanMode = it.getInt(BUNDLE_FAN_CONTROL_MODE)
            fanTemperature = it.getInt(BUNDLE_FAN_CONTROL_TEMPERATURE)
        }
    }

    private fun invalidateInput(text: String): Boolean{
        return text.isEmpty() || text == "+" || text == "-"
    }

    private fun setFanStateUI(){
        binding.ivOffSelect.visibility = View.GONE
        binding.ivTemperatureSelect.visibility = View.GONE
        binding.ivOnSelect.visibility = View.GONE
        binding.etFanOnTemperature.setBackgroundResource(R.drawable.shape_co_10_st_gray_1)
        binding.etFanOnTemperature.isEnabled = false
        binding.etFanOnTemperature.setText(fanTemperature.toString())
        when(FanControlEnum.getFromValue(selectFanMode)){
            FanControlEnum.OFF -> binding.ivOffSelect.visibility = View.VISIBLE
            FanControlEnum.TEMPERATURE_CONTROL -> {
                binding.ivTemperatureSelect.visibility = View.VISIBLE
                binding.etFanOnTemperature.setBackgroundResource(R.drawable.shape_co_10_st_1f39ff_1)
                binding.etFanOnTemperature.isEnabled = true
            }
            FanControlEnum.ON -> binding.ivOnSelect.visibility = View.VISIBLE
        }
    }

    private fun initListeners(){

        binding.layoutOff.setOnClickListener {
            selectFanMode = FanControlEnum.OFF.value
            binding.etFanOnTemperature.isEnabled = false
        }
        binding.layoutFanControl.setOnClickListener {
            selectFanMode = FanControlEnum.TEMPERATURE_CONTROL.value
            binding.etFanOnTemperature.isEnabled = true
        }
        binding.layoutOn.setOnClickListener {
            selectFanMode = FanControlEnum.ON.value
            binding.etFanOnTemperature.isEnabled = false
        }

        binding.etFanOnTemperature.doAfterTextChanged { editable ->
            binding.btnConfirm.isEnabled = !invalidateInput(editable.toString())
        }

        binding.btnConfirm.setOnClickListener {
            Log.i(LOG_TAG, "$selectFanMode ---${binding.etFanOnTemperature.text.toString().toInt()}")
            val bundle = bundleOf(
                BUNDLE_FAN_CONTROL_SELECT_INDEX to selectFanMode,
                BUNDLE_FAN_CONTROL_TEMPERATURE to binding.etFanOnTemperature.text.toString().toInt()
            )
            requireActivity().supportFragmentManager.setFragmentResult(REQUEST_FAN_CONTROL_KEY,bundle)
            findNavController().navigateUp()
        }
    }
    override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup): FragmentFanControlBinding {
        return FragmentFanControlBinding.inflate(inflater,container,true)
    }
}